//
//  HotWindowViewController.m
//  Beauty
//
//  Created by Snow on 16/9/13.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "HotWindowViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "DetaiHotWindowTableViewCell.h"
#import "ModelOfDetailWindow.h"
#import "ViewControllerOfHotWindow.h"
#import "LoginViewController.h"
@interface HotWindowViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfHotWindow;
@property (nonatomic, strong) NSDictionary *dictionary;
@property (nonatomic, strong) NSString *personal_id;
@property (nonatomic, strong) NSString *like_id;
@property (nonatomic, assign) BOOL isLike;
@end

@implementation HotWindowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
}
- (void)handleData
{
    _arrayOfHotWindow = [NSMutableArray array];
    //http://www.wantease.com/mobile1/index.php?act=index&op=hot_topic_info_1_5&class_id=1&key=979742e53b1003df4b0ecce6ba8a1168
    NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=index&op=showcase_detail&favorites_class_id=%@", _favorita_class_id];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        _dictionary = [NSDictionary dictionaryWithDictionary:dic[@"favorites_class_list"]];
        NSArray *arr = dic[@"micro_personal"];
        for (NSDictionary *dict in arr) {
            ModelOfDetailWindow *model = [[ModelOfDetailWindow alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfHotWindow addObject:model];
        }
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, SCREEN_WIDTH, 30)];
        UILabel *labelOfFans = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, SCREEN_WIDTH - 100, 20)];
        [view addSubview:labelOfName];
        [view addSubview:labelOfFans];
        labelOfName.text = _dictionary[@"favorites_class_name"];
        labelOfName.font = [UIFont systemFontOfSize:20];
        labelOfFans.text =[NSString stringWithFormat:@"粉丝 %@", _dictionary[@"favorites_count"]];
        labelOfFans.textColor = [UIColor lightGrayColor];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 90, 50, 50)];
        [image sd_setImageWithURL:[NSURL URLWithString:_dictionary[@"member_avatar"]]];
        image.layer.masksToBounds = YES;
        image.layer.cornerRadius = 25;
        [view addSubview:image];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(SCREEN_WIDTH - 90, 50, 80, 30);
        [button setTitle:@"+ 关注" forState:UIControlStateNormal];
        button.tintColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:0.822];
        [button addTarget:self action:@selector(handleCOntractWIndow:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 5;
        button.layer.borderColor = [[UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:0.822] CGColor];
        button.layer.borderWidth = 1;
        [view addSubview:button];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(70, 90, 200, 30)];
        [view addSubview:label];
        if (_dictionary[@"member_truename"] != nil) {
            NSString *str = [_dictionary[@"member_truename"] stringByAppendingString:@": "];
            label.text = [str stringByAppendingString:_dictionary[@"favorites_content"]];
        }
        _tableView.tableHeaderView = view;
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];

}
- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[DetaiHotWindowTableViewCell class] forCellReuseIdentifier:@"DetaiHotWindowTableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayOfHotWindow.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetaiHotWindowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetaiHotWindowTableViewCell"];
    ModelOfDetailWindow *model = [_arrayOfHotWindow objectAtIndex:indexPath.row];
    [cell.imageOfContent sd_setImageWithURL:[NSURL URLWithString:model.commend_image]];
    [cell.imageOfPerson sd_setImageWithURL:[NSURL URLWithString:model.member_avatar]];
    _isLike = model.is_like;
    if (_isLike == 1) {
        [cell.buttonOfLike setImage:[UIImage imageNamed:@"likeit"] forState:UIControlStateNormal];
    } else {
        [cell.buttonOfLike setImage:[UIImage imageNamed:@"buttonOfLove"] forState:UIControlStateNormal];
    }
    _personal_id = model.personal_id;
    _like_id = model.commend_member_id;
    cell.labelOfName.text = model.member_truename;
    cell.labelOfContent.text = model.commend_message;
    cell.labelOfContent.numberOfLines = 0;
    cell.labelOfLikeCount.text = model.like_count;
    cell.labelOfLikeCount.textColor = [UIColor lightGrayColor];
    [cell.buttonOfContract addTarget:self action:@selector(handleContractPerson:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonOfLike addTarget:self action:@selector(handleLike:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfDetailWindow *model = [_arrayOfHotWindow objectAtIndex:indexPath.row];
    ViewControllerOfHotWindow *view = [[ViewControllerOfHotWindow alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    view.person_id = model.personal_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:view animated:YES];
}
- (void)handleCOntractWIndow:(UIButton *)button
{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userdefault objectForKey:@"name"];
    NSString *password = [userdefault objectForKey:@"password"];
    if (name.length == 0) {
        LoginViewController *login = [[LoginViewController alloc]init];
[self presentViewController:login animated:YES completion:^{
    
}];
    } else {
        //http://www.wantease.com/mobile1/index.php?act=member_favorites_class&op=favorites_class_add&favorites_class_id=31&key=20fba81543630d1c232f03d5b9c2820b

        NSString *url = [NSString stringWithFormat:@" http://www.wantease.com/mobile1/index.php?act=member_favorites_class&op=favorites_class_add&favorites_class_id=%@&key=%@", _favorita_class_id, password];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"收藏成功" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        [alert dismissViewControllerAnimated:self completion:^{
            
        }];
    } filed:^(NSError *error) {
        
    }];
    }
}
- (void)handleContractPerson:(UIButton *)button
{
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    NSString *password = [userDefault objectForKey:@"password"];
    if (name == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];
        NSLog(@"%@",name);
[self presentViewController:login animated:YES completion:^{
    
}]  ;
    } else {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=sns_friend&op=add_follow&key=%@&member_id=%@", password, _personal_id];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已关注" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            
            // 出现
            [self presentViewController:alertController animated:YES completion:^{
                
            }];
            // 移除
            [alertController dismissViewControllerAnimated:YES completion:^{
            }];
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }
}
- (void)handleLike:(UIButton *)button
{
    [button setImage:[UIImage imageNamed:@"likeit"] forState:UIControlStateNormal];
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    NSString *password = [userDefault objectForKey:@"password"];
    if (name == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];
        NSLog(@"%@",name);
        [self presentViewController:login animated:YES completion:^{
            
        }];
        
    } else {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=sns_friend&op=add_follow&key=%@&member_id=%@", password, _like_id];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            NSString *string = responseObject[@"datas"][@"message"];
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:string message:@"" preferredStyle:UIAlertControllerStyleAlert];

            // 出现
            [self presentViewController:alertController animated:YES completion:^{
                
            }];
            // 移除
            [alertController dismissViewControllerAnimated:YES completion:^{
            }];
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}




@end
